package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.base.result.ResponseVo;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegistVO;
import com.atguigu.srb.core.pojo.vo.UserIndexVO;
import com.atguigu.srb.core.pojo.vo.UserInfoDetailVO;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-03-25
 */
@RestController
//@CrossOrigin
@RequestMapping("/api/core/userInfo")
public class ApiUserInfoController {
    @Resource
    UserInfoService userInfoService;
    @ApiOperation("查询会员个人中心数据")
    @GetMapping("/index")
    public ResponseVo userInfoIndex(@RequestHeader(value = "token",required = false)
                                            String token){
        UserIndexVO userIndexVO = userInfoService.getUserIndexVO(token);
        return ResponseVo.ok().data("item",userIndexVO);
    }
    @ApiOperation("给wx用户绑定手机号码和用户类型")
    @PutMapping("/bindMobile")
    public ResponseVo bindMobile(@RequestHeader(value = "token",required = false)
                                         String token,
                                 @RequestBody RegistVO registVO){
        userInfoService.bindMobile(token,registVO);
        return ResponseVo.ok().message("绑定成功");
    }

    @ApiOperation("判断用户是否绑定过手机号")
    @GetMapping("/isBindMobile")
    public ResponseVo isBindMobile(@RequestHeader(value = "token",required = false)
                                           String token){
        boolean flag = userInfoService.isBindMobile(token);
        return ResponseVo.ok().data("flag",flag);
    }
    @ApiOperation("判断手机号是否已被注册")
    @GetMapping("/isUsed/{mobile}")
    public ResponseVo isUsed(@PathVariable("mobile")String mobile){
        return ResponseVo.ok().data("flag",userInfoService.count(Wrappers.lambdaQuery(UserInfo.class)
                .eq(UserInfo::getMobile ,mobile))==0);//true表示手机号可用
    }

    @ApiOperation("根据token查询用户个人信息")
    @GetMapping("/details")//token存在请求头中
    public ResponseVo userInfoDetails(@RequestHeader(value = "token",required = false)
                                              String token){
        //获取用户id
        Long userId = JwtUtils.getUserId(token);
        UserInfo userInfo = userInfoService.getById(userId);
        UserInfoDetailVO userInfoDetailVO = new UserInfoDetailVO();
        BeanUtils.copyProperties(userInfo , userInfoDetailVO);
        return ResponseVo.ok().data("item",userInfoDetailVO);
    }
    //登录成功 使用用户信息创建jwttoken 返回
    @ApiOperation("登录")
    @PostMapping("login")
    public ResponseVo login(@RequestBody LoginVO loginVO,
                            HttpServletRequest request){
        String jwt = userInfoService.login(loginVO,request);
        return ResponseVo.ok().data("token",jwt);
    }
    @ApiOperation("注册")
    @PostMapping("register")
    public ResponseVo register(@RequestBody RegistVO registVO){
        userInfoService.saveUser(registVO);
        return ResponseVo.ok().message("注册成功");
    }
}

